﻿using Ndns.Packet.Abstractions;
using System.Collections.Generic;
using System.Linq;
using System.Collections;

namespace Ndns.Packet.Parser
{
    class DnsHostNameBuilder : IEnumerable<string>
    {
        private List<string> _negativeParts;
        private List<string> _parts;

        #region 构造

        public DnsHostNameBuilder()
        {
            _negativeParts = new List<string>();
            _parts = new List<string>();
        }
        public DnsHostNameBuilder(string domainPart)
        {
            _negativeParts = new List<string>();
            _parts = new List<string>();
            _parts.Add(domainPart);
        }
        public DnsHostNameBuilder(DnsHostNameBuilder builder)
        {
            _negativeParts = new List<string>();
            var domainParts = builder._parts;
            _parts = new List<string>(domainParts.Count);
            for (int i = 0; i < domainParts.Count; i++)
            {
                _parts.Add(domainParts[i]);
            }
        }
        public DnsHostNameBuilder(DnsHostName hostName)
        {
            _negativeParts = new List<string>();
            _parts = new List<string>(hostName.DomainParts.Count);
            foreach (var item in hostName)
            {
                _parts.Add(item);
            }
        }
        public DnsHostNameBuilder(IEnumerable<string> domainParts)
        {
            _negativeParts = new List<string>();
            _parts = new List<string>(domainParts);
        }

        #endregion

        #region 增加

        public DnsHostNameBuilder Append(string domainPart)
        {
            // TODO 检查增加的部分的合法性
            _parts.Add(domainPart);
            return this;
        }
        public DnsHostNameBuilder Append(IEnumerable<string> domainParts)
        {
            _parts.AddRange(domainParts);
            return this;
        }

        public DnsHostNameBuilder Prepend(string domainPart)
        {
            _negativeParts.Add(domainPart);
            return this;
        }
        public DnsHostNameBuilder Prepend(string[] domainParts)
        {
            for (int i = domainParts.Length - 1; i >= 0; i--)
            {
                _parts.Add(domainParts[i]);
            }
            return this;
        }
        public DnsHostNameBuilder Prepend(IReadOnlyList<string> domainParts)
        {
            for (int i = domainParts.Count - 1; i >= 0; i--)
            {
                _parts.Add(domainParts[i]);
            }
            return this;
        }
        public DnsHostNameBuilder Prepend(IEnumerable<string> domainParts)
        {
            _parts.AddRange(domainParts.Reverse());
            return this;
        }


        #endregion

        #region IEnumerable<string>实现


        public IEnumerator<string> GetEnumerator()
        {
            return GetEnumeratorCore();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumeratorCore();
        }

        private IEnumerator<string> GetEnumeratorCore()
        {
            for (int i = _negativeParts.Count - 1; i >= 0; i++)
            {
                yield return _negativeParts[i];
            }
            for (int i = 0; i < _parts.Count; i++)
            {
                yield return _parts[i];
            }
        }

        #endregion

        #region 返回

        public DnsHostName ToHostName()
        {
            return new DnsHostName(this);
        }

        public override string ToString()
        {
            // TODO 优化实现
            return (new DnsHostName(this)).ToString();
        }

        public static explicit operator string(DnsHostNameBuilder builder)
        {
            return builder.ToString();
        }

        #endregion

    }
}
